package com.fmi.cloud.biz;

import android.app.Activity;
import android.content.Context;

import com.fmi.cloud.R;
import com.fmi.cloud.act.MainActivity;
import com.fmi.cloud.util.IntentHelper;
import com.fmi.cloud.util.OrangeDataHandler;
import com.fmi.cloud.util.OrangeErrorHandler;
import com.fmi.cloud.util.StringUtil;
import com.fmi.cloud.util.VolleyHttpUtil;
import com.fmi.cloud.util.YcToastHelper;
import com.fmi.cloud.view.LoadDialog;
import com.fmi.thirdsdk.jpush.JPushUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ZeroVoid on 2018-3-9.
 */

public class CloudHttpBiz {

    public static void checkExist(final Context context, String username, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", username);
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(context, R.string.path_check_exist), map, callback);
    }

    /**
     * 本来应该写到不同build variant
     */
    public static void sendSms(final Context context, String phone, VolleyHttpUtil.ResponseCallBack callback) {
        LoadDialog.show(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("msgType", "1");
        VolleyHttpUtil.getInstance().setHeader("namespace", "152");
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(context, R.string.path_sms), map, callback);
    }

    //房米云
    public static void login(final Context ctx, final String phone, String smsCode) {
        if (StringUtil.isBlank(phone)) {
            YcToastHelper.getInstance()._toast("请先输入手机号");
            return;
        } else if (StringUtil.isBlank(smsCode)) {
            YcToastHelper.getInstance()._toast("请先输入验证码");
            return;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", phone);
        map.put("smscode", smsCode);
        map.put("namespace", "152");
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(ctx, R.string.path_login), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
                if (isOk) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
                    if (map != null) {
                        String userId = (String) map.get("userId");
                        CommonSp.setUserID(userId);
                        try {
                            String ret = response.getString("entity");
                            CommonSp.setUserInfo(ret);
                            getOperatorInfo(ctx, phone);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    private static void getOperatorInfo(final Context context, String tel) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", tel);
        LoadDialog.show(context);
        VolleyHttpUtil.getInstance().postWithCallback(FmHttpBiz.getStr(context, R.string.path_get_operator_info), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(context);
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response);
                if (isOk) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntity(response);
                    if (map != null) {
                        String operatorId = (String) map.get("operatorId");
                        CommonSp.setOperatorID(operatorId);
                        handleTags(context, operatorId);
                    }
                }
                YcToastHelper.getInstance()._toast("登录成功");
                FmHttpBiz.jumpMain(context);
                ((Activity) context).finish();
            }
        });
    }

    private static void handleTags(Context context, String operatorId) {
        Set<String> set = new HashSet<>();
        String userID = CommonSp.getUserID();
        userID = JPushUtil.isValidTagAndAlias(userID);
        if (userID != null) {
            set.add(userID);
        }
        operatorId = JPushUtil.isValidTagAndAlias(operatorId);
        if (operatorId != null) {
            set.add(operatorId);
        }
        JPushInterface.setTags(context, 0, set);
    }

}
